/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.diagram.activity.helper;

import org.eclipse.uml2.uml.Action;
import org.eclipse.uml2.uml.Activity;
import org.eclipse.uml2.uml.ActivityNode;
import org.eclipse.uml2.uml.Element;
import org.eclipse.uml2.uml.Interaction;

/**
 * Abstract class used to managed {@link ActivityNode}
 *
 * {@link Action}s may be created as child of other elements than {@link Activity}...
 * The case of {@link Interaction} is treated here, more generalization may be
 * necessary.
 */
public abstract class NodeActionHelper extends NodeActivityNodeHelper {


	/* (non-Javadoc)
	 * @see com.cea.papyrus.core.common.helper.NodeHelper#addUMLElement(org.eclipse.uml2.uml.Element, org.eclipse.uml2.uml.Element)
	 */
	@Override
	public boolean addUMLElement(Element element, Element umlParent) {
		if (umlParent instanceof Interaction) {
			return ((Interaction) umlParent).getActions().add((Action) element);
		} else {
			return super.addUMLElement(element, umlParent);
		}
	}

	/* (non-Javadoc)
	 * @see com.cea.papyrus.core.common.helper.NodeHelper#canCreateUMLElement(org.eclipse.uml2.uml.Element)
	 */
	@Override
	public boolean canCreateUMLElement(Element umlParent) {
		if (umlParent instanceof Interaction) {
			return true;
		} else {
			return super.canCreateUMLElement(umlParent);
		}
	}

	/* (non-Javadoc)
	 * @see com.cea.papyrus.core.common.helper.NodeHelper#removeUMLElement(org.eclipse.uml2.uml.Element, org.eclipse.uml2.uml.Element)
	 */
	@Override
	public boolean removeUMLElement(Element element, Element umlParent) {
		if (umlParent instanceof Interaction) {
			return ((Interaction) umlParent).getActions().remove(element);
		} else {
			return super.removeUMLElement(element, umlParent);
		}
	}
	
	@Override
	public Element createUMLElement(String name, Element umlParent){
		Element newAction = null;
		if (umlParent instanceof Interaction) {
			newAction = ((Interaction) umlParent).createAction(name, getNodeType() );
		} else {
			newAction = super.createUMLElement(name, umlParent);
		}
		return newAction;
	}

	/* (non-Javadoc)
	 * @see com.cea.papyrus.core.common.helper.NodeHelper#getNewUMLElementName(org.eclipse.uml2.uml.Element)
	 */
	@Override
	public String getNewUMLElementName(Element umlParent) {
		if (umlParent instanceof Interaction) {
			String name = ""; //$NON-NLS-1$
			
			// i <10000: avoid infinite loops
			for(int i=0; i< 10001; i++ ) {
				name = getBaseString()+i;
				if(null == ((Interaction) umlParent).getAction(name)) {
					return name;
				}
			}
			return getBaseString()+"X"; //$NON-NLS-1$
		} else {
			return super.getNewUMLElementName(umlParent);
		}
	}
	
}